package com.sunstar.cwcamerademo

import android.app.Application
import android.content.Intent
import android.os.Handler
import android.util.Log
import com.gitee.sunstar.adblib.ADBHelper
import com.hjjt.cameralib.CameraHelperImpl
import com.hjjt.cameralib.CameraInstance
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class App : Application() {
    val testViewModel = TestViewModel.instance
    override fun onCreate() {
        super.onCreate()
        DSHelper.initContext(this)

        CameraInstance.instance.applicationContext = this
//       `` CameraHelperImpl.cameraStartDetect = {
//            GlobalScope.launch {
//                withContext(Dispatchers.IO) {
//                    testViewModel.addTimes()
//                }
////                reBootTester()
//            }
//        }``
    }

    private fun reBootTester() {
        Log.e("CloudWalk", "reBootTester: ${testViewModel.type} ", )
        when (testViewModel.type) {
            TesterType.HOT_REBOOT -> Handler(mainLooper).postDelayed(
                {
                    val intent = Intent()
                    intent.setClass(this, TesterPage::class.java)
                    startActivity(intent)
                }, 2000
            )
            TesterType.COLD_REBOOT -> {
                GlobalScope.launch {
                    val complete = withContext(Dispatchers.IO) {
                        testViewModel.checkColdTesterComplete()
                    }
                    Log.e("CloudWalk", "reBootTester: ${complete} \n ${testViewModel.isTester.value}", )
                    if (testViewModel.isTester.value == true) {
                        ADBHelper.instance.reboot({}, { e -> })
                    }
                }
            }
        }
    }
}